package com.ruoyi.map.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.map.domain.BuildingInfo;
import com.ruoyi.map.domain.RegionInfo;
import com.ruoyi.map.domain.UnitInfo;
import com.ruoyi.map.domain.vo.BuildingVo;
import com.ruoyi.map.domain.vo.RegionVo;
import com.ruoyi.map.domain.vo.VillageVo;
import com.ruoyi.map.mapper.BuildingInfoMapper;
import com.ruoyi.map.mapper.RegionInfoMapper;
import com.ruoyi.map.mapper.UnitInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.map.mapper.VillageInforMapper;
import com.ruoyi.map.domain.VillageInfor;
import com.ruoyi.map.service.IVillageInforService;

import javax.annotation.Resource;

/**
 * 保障房建筑信息数据Service业务层处理
 *
 * @author dihang
 * @date 2023-07-24
 */
@Service
public class VillageInforServiceImpl extends ServiceImpl<VillageInforMapper, VillageInfor> implements IVillageInforService {

    @Resource
    VillageInforMapper villageInforMapper;
    @Resource
    RegionInfoMapper regionInfoMapper;
    @Resource
    BuildingInfoMapper buildingInfoMapper;
    @Resource
    UnitInfoMapper unitInfoMapper;

    /**
     * 查询保障房建筑信息数据列表
     *
     * @return 保障房建筑信息数据
     */
    @Override
    public List<VillageInfor> selectVillageInforList() {
        List<VillageInfor> list = villageInforMapper.selectList(null);
        if (list.isEmpty()) {
            throw new BaseException("数据为空！");
        }
        for (VillageInfor villageInfor : list) {
            Long id = villageInfor.getId();
            List<RegionInfo> regionInfos = regionInfoMapper.selectList(Wrappers.<RegionInfo>lambdaQuery().eq(RegionInfo::getVillageId, id));
            int size = regionInfos.size();
            //计算小区包含的区域数量
            villageInfor.setIncludeRegionNum(size);
            //将该小区所包含的所有区域整理成一个集合
            List<String >nameList=regionInfos.stream().map(RegionInfo::getName).collect(Collectors.toList());
            //进行赋值
            villageInfor.setIncludeRegionName(nameList);
        }

        return list;
    }

    /**
     * 新增保障房建筑信息数据
     *
     * @param villageInfor
     */
    @Override
    public void addNew(VillageInfor villageInfor) {
        if (villageInfor.getId() == null) {
            throw new BaseException("主键不能为空！");
        }
        LambdaQueryWrapper<VillageInfor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VillageInfor::getName, villageInfor.getName());
        queryWrapper.eq(VillageInfor::getDeleted, "0");  //deleted为是否删除 0 否 1 是
        Long count = villageInforMapper.selectCount(queryWrapper);
        //保障房一个小区只能录入一次
        if (count > 0) {
            throw new BaseException("该小区已存在，不能重复录入！");
        }
        this.save(villageInfor);

    }

    /**
     * 分页-条件查询保障房建筑信息数据列表
     *
     * @param villageInfor
     * @return
     */
    @Override
    public IPage<VillageInfor> villageSearch(VillageInfor villageInfor) {
        //按条件分页查询
        IPage<VillageInfor> page = this.page(new Page<>(villageInfor.getPageNum(), villageInfor.getPageSize()), oneSelectWhere(villageInfor));
        //转化为List集合
        List<VillageInfor> villageInforList = page.getRecords();
        List<VillageInfor>list=new ArrayList<>();
        for (VillageInfor infor : villageInforList) {
            Long id = infor.getId();
            List<RegionInfo> regionInfos = regionInfoMapper.selectList(Wrappers.<RegionInfo>lambdaQuery().eq(RegionInfo::getVillageId, id));
            int size = regionInfos.size();
            //计算小区包含的区域数量
            infor.setIncludeRegionNum(size);
            //将该小区所包含的所有区域整理成一个集合
            List<String >nameList=regionInfos.stream().map(RegionInfo::getName).collect(Collectors.toList());
            //进行赋值
            infor.setIncludeRegionName(nameList);
            list.add(infor);
        }
        IPage<VillageInfor>iPage=new Page<>();
        iPage.setRecords(list);
        return iPage;
    }


    /**
     * 条件查询 封装QueryWrapper
     *
     * @param villageInfor
     */
    private LambdaQueryWrapper<VillageInfor> oneSelectWhere(VillageInfor villageInfor) {
        LambdaQueryWrapper<VillageInfor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (villageInfor == null) {
            return lambdaQueryWrapper;
        }
        lambdaQueryWrapper.like(StrUtil.isNotBlank(villageInfor.getName()), VillageInfor::getName, villageInfor.getName())
                .eq(VillageInfor::getDeleted, "0");
        lambdaQueryWrapper.orderByDesc(VillageInfor::getCreateTime);
        return lambdaQueryWrapper;
    }

    /**
     * 获取保障房建筑信息数据详细信息
     * @param id
     * @return
     */
    @Override
    public VillageInfor selectById(Long id) {
        VillageInfor villageInfor = this.getById(id);
        Long id1 = villageInfor.getId();
        List<RegionInfo> regionInfos = regionInfoMapper.selectList(Wrappers.<RegionInfo>lambdaQuery().eq(RegionInfo::getVillageId, id1));
        int size = regionInfos.size();
        //计算小区包含的区域数量
        villageInfor.setIncludeRegionNum(size);
        //将该小区所包含的所有区域整理成一个集合
        List<String >nameList=regionInfos.stream().map(RegionInfo::getName).collect(Collectors.toList());
        //进行赋值
        villageInfor.setIncludeRegionName(nameList);
        return villageInfor;
    }


    /**
     * 根据小区名称查询所包含的标注点
     * @param name
     * @return
     */
    @Override
    public VillageVo selectName(String name) {
        VillageInfor villageInfor = this.getOne(Wrappers.<VillageInfor>lambdaQuery().eq(VillageInfor::getName, name));
        Long id1 = villageInfor.getId();
        List<RegionInfo> regionInfos = regionInfoMapper.selectList(Wrappers.<RegionInfo>lambdaQuery().eq(RegionInfo::getVillageId, id1));
        int size = regionInfos.size();
        //计算小区包含的区域数量
        villageInfor.setIncludeRegionNum(size);
        //将该小区所包含的所有区域整理成一个集合
        List<String >nameList=regionInfos.stream().map(RegionInfo::getName).collect(Collectors.toList());
        //进行赋值
        villageInfor.setIncludeRegionName(nameList);

        VillageVo villageVo=new VillageVo();
        BeanUtils.copyProperties(villageInfor,villageVo);
        List<RegionVo>regionVoList=new ArrayList<>();

        for (RegionInfo regionInfo : regionInfos) {
            //区域id
            Long id = regionInfo.getId();
            //查询出该区域所有的楼栋
            List<BuildingInfo> buildingInfoList=buildingInfoMapper
                    .selectList(Wrappers.<BuildingInfo>lambdaQuery().eq(BuildingInfo::getRegionId,id));
            //计算楼栋数量
            int size1 = buildingInfoList.size();
            //进行赋值
            regionInfo.setIncludeBuildingNum(size1);
            //查询楼栋名称集合
            List<String> buildingName = buildingInfoList.stream().map(BuildingInfo::getName).collect(Collectors.toList());
            regionInfo.setIncludeBuildingName(buildingName);

            RegionVo regionVo=new RegionVo();
            BeanUtils.copyProperties(regionInfo,regionVo);
            regionVoList.add(regionVo);
            villageVo.setRegionVoList(regionVoList);

            List<BuildingVo>buildingVoList=new ArrayList<>();
            for (BuildingInfo buildingInfo : buildingInfoList) {
                Long idd = buildingInfo.getId();
                //查询出该楼栋所有的单元数据
                List<UnitInfo> unitInfos = unitInfoMapper.selectList(Wrappers.<UnitInfo>lambdaQuery().eq(UnitInfo::getBuildingId, idd));
                int size2 = unitInfos.size();
                //计算楼栋包含的单元数量
                buildingInfo.setIncludeUnitNum(size2);
                //将该楼栋所包含的所有单元整理成一个集合
                List<String >nameLists=unitInfos.stream().map(UnitInfo::getName).collect(Collectors.toList());
                //进行赋值
                buildingInfo.setIncludeUnitName(nameLists);

                BuildingVo buildingVo=new BuildingVo();
                BeanUtils.copyProperties(buildingInfo,buildingVo);
                buildingVoList.add(buildingVo);
                regionVo.setBuildingVoList(buildingVoList);

                List<UnitInfo>unitInfoList=new ArrayList<>();
                for (UnitInfo unitInfo : unitInfos) {
                    unitInfoList.add(unitInfo);
                    buildingVo.setUnitInfoList(unitInfoList);
                }

            }

        }

        return villageVo ;
    }

}
